package com.hmall.order.amqp;


import com.hmall.common.myClient.ItemClient;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.service.IOrderService;
import com.hmall.order.service.OrderDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Slf4j
public class DirecterListener {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ItemClient itemClient;


    @RabbitListener(queues = MqConstants.ORDER_DEAD_QUEUE)
    public void listenOrderQueue(Long orderid) {
        log.info("延迟消息过来了，时间和id是：{}", new Date() +"-----"+orderid);
        Order order = orderService.getById(orderid);
        OrderDetail orderDetail = orderDetailService.getByOrderId(orderid);
        Long itemId = orderDetail.getItemId();
        Integer num = orderDetail.getNum();
        if (order.getStatus() == 1) {
            num = -(num);
            //，如果未支付：肯定是超时未支付订单，将其**status修改为5**，取消订单
            order.setStatus(5);
            orderService.updateById(order);
            // 恢复扣减的库存
            itemClient.reduceInventory(itemId,num);
        }
    }




}
